//
//  LBPayMoneyController.m
//  loveBeen
//
//  Created by 郭小双 on 16/11/20.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBPayMoneyController.h"
#import "LBProductCell.h"
#import "LBPayWayCell.h"
#import "LBPayFootView.h"
#import <SVProgressHUD.h>
#import "LBCouponController.h"
#define manager [LBPlusMinusTool sharedPlusMinusTool]
static NSString *prouductCellID = @"prouductCell";
@interface LBPayMoneyController ()<UITableViewDataSource,UIScrollViewDelegate,UITableViewDelegate>
@property(nonatomic,weak)UITableView *tableView;
@end

@implementation LBPayMoneyController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.navigationItem.title = @"支付";
    
}

- (void)setupUI{
    UITableView *tableV = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableV.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:tableV];
    _tableView = tableV;
    tableV.delegate = self;
    tableV.dataSource = self;
    [tableV registerNib:[UINib nibWithNibName:@"LBDiscountCardCell" bundle:nil] forCellReuseIdentifier:@"youhui"];
    
     [tableV registerNib:[UINib nibWithNibName:@"LBPayWayCell" bundle:nil] forCellReuseIdentifier:@"payWay"];
    
    [tableV registerClass:[LBProductCell class] forCellReuseIdentifier:prouductCellID];
    
    tableV.estimatedRowHeight = 180;
    tableV.rowHeight = UITableViewAutomaticDimension;
    
    
   LBPayFootView  *footV = [[[UINib nibWithNibName:@"LBPayFootView" bundle:nil]instantiateWithOwner:self options:0]firstObject];
    
    footV.price.text = [NSString stringWithFormat:@"¥%@",@([manager selectedProductPrice]+5).description];
    
    [footV.pay addTarget:self action:@selector(Pay) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableFooterView = footV;
    
}

-(void)Pay{
    
    [manager.selectedProductsModel enumerateObjectsUsingBlock:^(LBSuperMarketProducts * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(obj.selected){
            
            [manager.payProductModel addObject:obj];
            
            obj.totalCount = 0;
            obj.selected = NO;
        }
    }];
    
    [manager.selectedProductsModel removeObjectsInArray:manager.payProductModel];
    
    
    [SVProgressHUD showWithStatus:@"正在支付,请稍后...."];
   
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self.navigationController popViewControllerAnimated:YES];
    });
    manager.change = YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0 || section == 1 ){
        return 1;
    } else if(section == 2){
        return [manager kindsOfSelectedProduct]+2;
    }
    return 4;
}



- (CGFloat)tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    NSString *identify = prouductCellID;
    if(indexPath.section == 0 ){
        identify = @"youhui";
    }
    
    LBProductCell *cell = [tableView dequeueReusableCellWithIdentifier:identify forIndexPath:indexPath];
    if(indexPath.section == 1){
        LBPayWayCell *payWayCell = [tableView dequeueReusableCellWithIdentifier:@"payWay" forIndexPath:indexPath];
        return payWayCell;
    }
  
        if(indexPath.row == 0 && indexPath.section == 2){
        cell.nameLab.text = @"精选商品";
        cell.nameLab.textColor = [UIColor grayColor];
        cell.priceLab.hidden = YES;
        cell.countLab.hidden = YES;
        return cell;
        }
//

    if(indexPath.section == 2 && indexPath.row == [manager kindsOfSelectedProduct]+1){
        cell.nameLab.hidden = YES;
        cell.countLab.hidden = YES;
        
        cell.priceLab.text = [NSString stringWithFormat:@"合计：%.2f",[manager selectedProductPrice]];
        cell.priceLab.textColor = [UIColor redColor];
        return cell;
    }
    
    if(indexPath.section == 3){
        float price = [manager selectedProductPrice];
        NSArray<NSString *> *detailName = @[@"商品总额",@"配送费",@"服务费",@"优惠券"];
        NSArray<NSNumber *> *priceLab = @[@(price),@5,@0,@0];
        cell.countLab.hidden = YES;
        if(indexPath.row == 0){
            cell.nameLab.text = @"费用明细";
            cell.nameLab.textColor = [UIColor grayColor];
            cell.priceLab.hidden = YES;
        }
        for(NSUInteger i = 0;i < 4; i++){
            if(indexPath.row == i + 1){
                cell.nameLab.text = detailName[i];
                cell.priceLab.text = [NSString stringWithFormat:@"¥%.2lf",priceLab[i].floatValue];
            }
        }
        
        return cell;
    }
    if (indexPath.section == 2) {
        cell.model = manager.selectedProductsModel[indexPath.row - 1];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section ==0){
        LBCouponController *couVc = [[LBCouponController alloc]init];
        [self.navigationController pushViewController:couVc animated:YES];
    }
}

@end
